var parser = {};

$(function(){
	//console.log('at parse');
	parser.parseQuestions = function () {
		var qtext = [];
		var atext = [];
		var data = $('body').find('.content').find('.main-text-area').val();
		//console.log(data);
		data = data.split("\n");
		for(var i = 0; i < data.length; i++)
		{
			var temp = data[i].split(",");
			qtext[i] = temp[0];
			atext[i] = temp[1];
		}
		return {'qtext': qtext, 'atext': atext};
	};
	
	parser.writeQuiz = function (def/*[]*/, ans/*[]*/, num) {
		//starts quiz
		var bank = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<quiz>\n";
		var feedback = [];
		var remainder = num%def.length;
		var num_per = Math.floor(num/def.length);
		//console.log(Math.floor(num/def.length), num, def.length);
		
		for(var i = 0; i < def.length; i++)
		{
			feedback[i] = " ";
			var ansTemp = [];
			var added = 0;
			for(var j = 0; j < ans.length ;j++)
			{
				if(j!=i)
				{
					ansTemp[added] = ans[j];
					added++;
				}
			}
			if(remainder > 0) {
				var answers = randomAnswers(ansTemp, num_per + 1);
				console.log(num_per, 'added one');
				remainder--;
			} else {
				console.log(num_per);
				var answers = randomAnswers(ansTemp, num_per);
			}
			var correctNum = 3;
			for(var k = 0; k < answers.length; k++)
			{
				answers[k].push(ans[i]);
			}
			var name = "Question " + i;
			$.each(answers, function (index, element) {
				element.push(ans[i]);
				bank += writeMCQuestion(name, def[i], element, feedback[i], correctNum);
			});	
		}
		//other question formats 
		
		
		//ends quiz
		//console.log("at end quiz");
		bank += "\n</quiz>\n";
		return bank;
	};

	//randomAnswers(["blah1", "blah2", "blah3", "blah4", "blah5", "blah6", "blah7", "blah8", "blah9", "blah10"])

	var randomAnswers = function (ratext/*[]*/, max)
	{
			var len = ratext.length;
			var retVal = [];			
			var n = 0;
			
			//Three for loops (choose three)
			for (var i = 0; i < len - 2; i++)
			{
				for (var j = i + 1; j < len - 1; j++)
				{
					for (var k = j + 1; k < len; k++) {
						retVal.push([ratext[i], ratext[j], ratext[k]])
						n++;
						if(n >= max) {
							return retVal;
						}
					}
				}
			}
			//console.log(retVal);
			return retVal;
	};

	var writeMCQuestion = function (name, qtext, watext/*[]*/, feedback/*[]*/, correctNum)
	{
		var string = "";
		//write MC header
		string += "\t<question type =\"multichoice\">\n";
		//write Name
		string += "\t\t<name>\n\t\t\t<text> <![CDATA[<p>" + name + "</p>]]></text>\n\t\t</name>\n";
		//write Text
		string += "\t\t<questiontext format =\"html\">\n\t\t";
		string += "<text> <![CDATA[<p>" + qtext + "</p>]]></text>\n\t\t</questiontext>\n";
		//write Answer
		for(var i = 0; i < 4; i++)
		{
			if(i == correctNum)
			{
				string += writeMCAnswer(true, watext[i], feedback[i]);
			}
			else
			{
				string += writeMCAnswer(false, watext[i], feedback[i]);
			}
		}
		//write footer
		string += "\t\t<shuffleanswers>1</shuffleanswers>\n\t\t<single>true</single>\n\t\t<answernumbering>ABCD</answernumbering>\n\t</question>\n";
		return string;
	};

	var writeMCAnswer = function (correct, text, feedback)
	{
		var answer = "";
		answer += "\t\t<answer fraction=\"";
		if(correct)
		{	
			answer += "100";
		}
		else
		{
			answer += "0";
		}
		
		answer += "\">\n\t\t\t\t<text> <![CDATA[<p>" + text + "</p>]]></text>\n\t\t\t\t<feedback><text>" + feedback + "</text></feedback>\n\t\t</answer>\n";
		return answer;
	};
	
	var writeCalcQuestion = function(name, qtext, watext/*[]*/, ans/*[]*/, num, tolerance){
		var temp_string = "";
		
		//write Calculated header
		temp_string += "\t<question type=\"calculated\">\n";
		
		//write Name
		temp_string += "\t\t<name>\n\t\t\t<text> <![CDATA[<p>" + name + "</p>]]></text>\n\t\t</name>\n";
		
		//write Text
		temp_string += "\t\t<questiontext format =\"html\">\n\t\t";
		temp_string += "<text> <![CDATA[<p>" + qtext + "</p>]]></text>\n\t\t</questiontext>\n";
		temp_string += "<penalty> 0 </penalty>\n<hidden> 0 </hidden>\n<syncronize> 0 </syncronize>\n<single> 0 </single>\n<answernumbering> abc </answernumbering>\n";
		temp_string += "<shuffleanswers> 1 </shuffleanswers>"
		
		//write answer
		temp_string += "<answer fraction=\"100\">\n\t<text> <![CDATA[<p> " + ans + " </p>]]></text>\n\t\t<tolerance> " + tolerance + " </tolerance>\n\t\t<tolerancetype> 1 </toleranctype>\n";
		temp_string += "\t\t<correctanswerformat> 1 </correctanswerformat>\n\t\t<correctanswerlength> 2 </correctanswerlength>\n</answer>\n";
		
		//write unit grading
		temp_string += "\t\t<unitgradingtype> 0 </unitgradingtype>\n\t\t<unitpenalty> 0.1 </unitpenalty>\n\t\t<showunits> 3 </showunits>\n\t\t<unitsleft> 0 </unitsleft>\n";
		
		//write dataset-repeat for each variable
		temp_string += "<\t\tdataset_definition>\n\t\t\t<status>\n\t\t\t\t<text> private </text>\n\t\t\t</status>\n\t\t\t<name>\n\t\t\t\t<text> " + var_name[0] + " </text>\n";
		temp_string += "\t\t\t</name>\n\t\t\t<type> calculated </type>\n\t\t\t<distribution>\n\t\t\t\t<text> uniform </text>\n\t\t\t</distribution>\n\t\t\t<minimum>\n\t\t\t\t<text> ";
		temp_string += min[0] + " </text>\n\t\t\t<maximum>\n\t\t\t\t<text> " + max[0] + " </text>\n\t\t\t</maximum>\n\t\t\t<decimals>\n\t\t\t\t<text> 1 </text>\n\t\t\t</decimals>\n\t\t\t<itemcount>";
		temp_string += "1 </itemcount>\n\t\t\t<dataset_items>\n\t\t\t\t<dataset_item>\n\t\t\t\t\t<number> 1 </number>\n\t\t\t\t\t<value> 2 </value>\n\t\t\t\t</dataset_item>\n\t\t\t\n<number_of_items>";
		temp_string += " 1 </number_of_items>\n\t\t\t</dataset_definition>";
		
		return temp_string;
	}
});
